var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();

Page(common.extendPage(PageBase, {
  data: {
    tabs: ['全部', '进行中', '已完成'],
    active: 0,
    isSeller: false,
    allLoaded: false,
    isLoadMore: false,
    noMore: false,
    dataLoaded: false,
    orderList: [],
    page: 1,
    loading: true
  },
  /**
   * 页面重载
   */
  onShow() {
    this.flush(true);
  },
  /**
   * 切换标签
   */
  _switchTab(evt) {
    var index = evt.currentTarget.dataset.index;
    this.setData({
      active: index
    })
    this.flush();
  },
  /**
   * 商家接受或拒绝服务
   */
  _orderHandler(evt) {
    var paramsUrl = '/public/api/orders/acceptOrder';
    var {
      orderId,
      accept
    } = evt.currentTarget.dataset;
    var paramsData = {
      uid: wx.getStorageSync('uid'),
      oid: orderId,
      is_accept: accept
    };
    ajax.P({
      url: paramsUrl,
      data: paramsData,
      success: (res, isLoadcb) => {
        if (accept == 1) {
          util.tips('商家已接受');
        } else {
          util.tips('商家已拒绝');
        }
        isLoadcb && isLoadcb();
      }
    })
  },

  /**
   * 下拉加载
   */
  onReachBottom() {
    this.loadMore()
  },
  /**
   *刷新
   */
  flush(isInit) {
    this.setData({
      page: 1,
      allLoaded: false,
      noMore: false
    });
    this.getOrderList(true, false, () => {
      wx.stopPullDownRefresh();
      if (isInit) {
        this.setData({
          loading: false
        })
      }
    });
  },

  /**
   * 加载更多
   */
  loadMore() {
    var {
      allLoaded
    } = this.data;

    if (allLoaded) {
      return;
    } else {
      this.setData({
        isLoadMore: true
      })
      this.getOrderList(false, true, () => {
        this.setData({
          dataLoaded: true,
          isLoadMore: false
        })
      });
    }
  },
  /**
   * 获取订单列表
   */
  getOrderList(isFlush, unneedLoad, cb) {
    var page = this.data.page;
    var paramsUrl = '/public/api/orders/morderlist';
    var orderList = this.data.orderList;
    var paramsData = {
      uid: wx.getStorageSync('uid'),
      filter: this.data.active,
      page,
      num: 10
    }

    ajax.P({
      url: paramsUrl,
      data: paramsData,
      success: (res, isLoadcb) => {
        var listData = res.data || [];
        var allLoaded = false;
        var noMore = false;
        if (listData.length == 0) {
          allLoaded = true;
          noMore = true;
        }
        if (isFlush) {
          orderList = listData;
        } else {
          orderList = orderList.concat(listData);
        }
        orderList.forEach((item, index) => {
          item.amount = parseFloat(item.amount).toFixed(2)
        })
        this.setData({
          orderList,
          page: ++page,
          allLoaded,
          noMore
        })
        isLoadcb && isLoadcb();
        cb && cb();
      },
      fail: (res) => {
        console.log(res);
      },
      unneedLoad
    })
  }
}));